package cn.hwadee.muying.service;

import cn.hwadee.muying.entity.Organization;
import cn.hwadee.muying.entity.Salary;
import cn.hwadee.muying.entity.WorkerLevel;
import cn.hwadee.muying.mapper.SalaryMapper;
import cn.hwadee.muying.commons.base.BaseService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.stereotype.Service;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Carlos
 * @since 2018-08-07
 */
@Service
public class SalaryService extends BaseService<SalaryMapper, Salary> {

    @Autowired
    private SalaryMapper salaryMapper;

    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private WorkerLevelService workerLevelService;

    public MyPage<Salary> list(MyPage<Salary> page, User cUser,String keyword) throws Exception{
        List<Salary> salarys = salaryMapper.list(page,cUser,keyword);
        for(Salary salary:salarys){
            //Organization org=organizationService.selectById(salary.getOrgId());
            //System.out.print(salary.getOrgId());
            //salary.setOrgName(org.getName());
            WorkerLevel temp=workerLevelService.selectById(salary.getWorkerLevelId());
            salary.setWorkerLevelName(temp.getName());
        }
        page.setRecords(salarys);
        return page;
    }

    public Salary selectByLevelId(int levelId) {
        List<Salary> salaries= salaryMapper.selectList(
                new EntityWrapper<Salary>()
                        .and("worker_level_id="+levelId)
                        .and("valid=1")
        );
        if(salaries!=null&&salaries.size()>0){
            return salaries.get(0);
        }else{
            return null;
        }
    }
}
